package com.css.fxfzaqbz.modules;


import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import java.io.*;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.ss.usermodel.Drawing;
import java.util.List;


public class Test {

    public static void main(String[] args) {
        try {
            // 创建新文档，用于合并
            XWPFDocument mergedDocument = new XWPFDocument();

            // 读取原始文档1
            XWPFDocument document1 = new XWPFDocument(new FileInputStream("document1.docx"));
            copyContentAndImages(document1, mergedDocument);

            // 读取原始文档2
            XWPFDocument document2 = new XWPFDocument(new FileInputStream("document2.docx"));
            copyContentAndImages(document2, mergedDocument);

            // 保存合并后的文档
            FileOutputStream outputStream = new FileOutputStream("merged_document.docx");
            mergedDocument.write(outputStream);
            outputStream.close();

            System.out.println("文档合并完成！");
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
    }

    private static void copyContentAndImages(XWPFDocument sourceDocument, XWPFDocument targetDocument) throws IOException, InvalidFormatException {
        // 复制文本内容
        targetDocument.createParagraph();

        // 遍历文本段落
        for (XWPFParagraph paragraph : sourceDocument.getParagraphs()) {
            // 复制段落样式
            XWPFParagraph newParagraph = targetDocument.createParagraph();
            newParagraph.getCTP().setPPr(paragraph.getCTP().getPPr());

            // 复制段落文本
            for (XWPFRun run : paragraph.getRuns()) {
                XWPFRun newRun = newParagraph.createRun();
                newRun.getCTR().setRPr(run.getCTR().getRPr());
                newRun.setText(run.getText(0));
            }
        }

        // 遍历文本表格
        for (XWPFTable table : sourceDocument.getTables()) {
            // 复制表格
            XWPFTable newTable = targetDocument.createTable();
            newTable.getCTTbl().setTblPr(table.getCTTbl().getTblPr());

            // 复制表格内容
            for (int i = 0; i < table.getRows().size(); i++) {
                XWPFTableRow newRow = newTable.createRow();
                XWPFTableRow sourceRow = table.getRow(i);
                newRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr());

                for (int j = 0; j < sourceRow.getTableCells().size(); j++) {
                    XWPFTableCell newCell = newRow.getCell(j);
                    XWPFTableCell sourceCell = sourceRow.getCell(j);

                    // 复制单元格样式
                    newCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());

                    // 复制单元格文本
                    for (XWPFParagraph paragraph : sourceCell.getParagraphs()) {
                        XWPFParagraph newParagraph = newCell.addParagraph();
                        newParagraph.getCTP().setPPr(paragraph.getCTP().getPPr());

                        for (XWPFRun run : paragraph.getRuns()) {
                            XWPFRun newRun = newParagraph.createRun();
                            newRun.getCTR().setRPr(run.getCTR().getRPr());
                            newRun.setText(run.getText(0));
                        }
                    }



                }
            }

            // 复制图片
            List<XWPFPictureData> pictures = sourceDocument.getAllPictures();
            for (XWPFPictureData picture : pictures) {
                byte[] byteData = picture.getData();
                String filename = picture.getFileName();
                int format =  getPictureType1(filename.substring(filename.lastIndexOf('.') + 1));
                targetDocument.addPictureData(byteData, format);
               // targetDocument.createPicture(targetDocument.getNextPicNameNumber(format), targetDocument.getAllPictures().size() - 1, 200, 200);
            }

        }
    }

    private static int getPictureType1(String fileExtension) {
        switch (fileExtension.toLowerCase()) {
            case "jpg":
            case "jpeg":
                return Document.PICTURE_TYPE_JPEG;
            case "png":
                return Document.PICTURE_TYPE_PNG;
            case "gif":
                return Document.PICTURE_TYPE_GIF;
            default:
                return Document.PICTURE_TYPE_BMP;
        }
    }

}
